#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <filesystem>

namespace fs = std::filesystem;

class DevOpsManager {
public:
    DevOpsManager() {
        project_root_ = "D:/vc/devops-platform";
    }
    
    void showStatus() {
        std::cout << "🚀 DevOps Platform Status\n";
        std::cout << "========================\n\n";
        
        // 检查构建状态
        checkBuildStatus();
        
        std::cout << "\nQuick Commands:\n";
        std::cout << "  build vs2022    - Build with Visual Studio 2022\n";
        std::cout << "  build msys2     - Build with MSYS2\n";
        std::cout << "  run             - Run the application\n";
        std::cout << "  clean           - Clean build files\n";
        std::cout << "  status          - Show this status\n";
    }
    
    void buildWithVS2022() {
        std::cout << "🔨 Building with Visual Studio 2022...\n";
        std::string command = "cd \"" + project_root_ + "/tools\" && build-manager-fixed.exe vs2022";
        system(command.c_str());
    }
    
    void buildWithMSYS2() {
        std::cout << "🔨 Building with MSYS2...\n";
        std::string command = "cd \"" + project_root_ + "/tools\" && build-manager-fixed.exe msys2";
        system(command.c_str());
    }
    
    void runApplication() {
        std::cout << "🚀 Running DevOps Platform...\n\n";
        
        // 检查各种可能的构建路径
        std::vector<std::string> possible_paths = {
            "build/Release/devops-platform.exe",
            "build-msys2/devops-platform.exe", 
            "build/devops-platform.exe"
        };
        
        bool found = false;
        for (const auto& path : possible_paths) {
            std::string full_path = project_root_ + "/" + path;
            if (fs::exists(full_path)) {
                std::cout << "📍 Found: " << path << "\n";
                std::cout << "----------------------------------------\n";
                system(("\"" + full_path + "\"").c_str());
                std::cout << "----------------------------------------\n";
                found = true;
                break;
            }
        }
        
        if (!found) {
            std::cout << "❌ No built executable found. Please build first.\n";
            std::cout << "💡 Run: devops-manager build vs2022\n";
        }
    }
    
    void cleanBuild() {
        std::cout << "🧹 Cleaning build files...\n";
        
        std::vector<std::string> dirs_to_remove = {
            "build",
            "build-msys2",
            "CMakeFiles",
            "CMakeCache.txt",
            "cmake_install.cmake"
        };
        
        for (const auto& dir : dirs_to_remove) {
            std::string full_path = project_root_ + "/" + dir;
            if (fs::exists(full_path)) {
                if (fs::is_directory(full_path)) {
                    fs::remove_all(full_path);
                    std::cout << "✅ Removed: " << dir << "\n";
                } else {
                    fs::remove(full_path);
                    std::cout << "✅ Removed: " << dir << "\n";
                }
            }
        }
        
        std::cout << "✅ Clean completed!\n";
    }
    
    void showUsage() {
        std::cout << "🤖 DevOps Platform Manager\n";
        std::cout << "==========================\n\n";
        std::cout << "Usage: devops-manager [command]\n\n";
        std::cout << "Commands:\n";
        std::cout << "  status          - Show platform status\n";
        std::cout << "  build vs2022    - Build with Visual Studio 2022\n";
        std::cout << "  build msys2     - Build with MSYS2 MinGW\n";
        std::cout << "  run             - Run the application\n";
        std::cout << "  clean           - Clean build files\n";
        std::cout << "  help            - Show this help\n\n";
        std::cout << "Examples:\n";
        std::cout << "  devops-manager status\n";
        std::cout << "  devops-manager build vs2022\n";
        std::cout << "  devops-manager run\n";
    }

private:
    std::string project_root_;
    
    void checkBuildStatus() {
        std::vector<std::pair<std::string, std::string>> build_paths = {
            {"build/Release/devops-platform.exe", "VS2022 Release"},
            {"build-msys2/devops-platform.exe", "MSYS2 Build"},
            {"build/devops-platform.exe", "Generic Build"}
        };
        
        bool has_build = false;
        for (const auto& [path, description] : build_paths) {
            std::string full_path = project_root_ + "/" + path;
            if (fs::exists(full_path)) {
                std::cout << "✅ " << description << ": Available\n";
                has_build = true;
            }
        }
        
        if (!has_build) {
            std::cout << "❌ No builds available\n";
            std::cout << "💡 Run: devops-manager build vs2022\n";
        } else {
            std::cout << "✅ Platform is ready to run!\n";
        }
    }
};

int main(int argc, char* argv[]) {
    DevOpsManager manager;
    
    if (argc < 2) {
        manager.showStatus();
        return 0;
    }
    
    std::string command = argv[1];
    
    if (command == "status") {
        manager.showStatus();
    } else if (command == "build" && argc >= 3) {
        std::string build_type = argv[2];
        if (build_type == "vs2022") {
            manager.buildWithVS2022();
        } else if (build_type == "msys2") {
            manager.buildWithMSYS2();
        } else {
            std::cout << "❌ Unknown build type: " << build_type << "\n";
            std::cout << "💡 Use: vs2022 or msys2\n";
        }
    } else if (command == "run") {
        manager.runApplication();
    } else if (command == "clean") {
        manager.cleanBuild();
    } else if (command == "help") {
        manager.showUsage();
    } else {
        std::cout << "❌ Unknown command: " << command << "\n\n";
        manager.showUsage();
        return 1;
    }
    
    return 0;
}
